<?php

namespace App\Lib;

use App\Http\Model\Hint;

class XFYunVoice
{
    public $requestData = [];// 接收参数

    private static $instance;

    // 配置
    protected $selfConfig = [];

    // 构造
    private function __construct()
    {
        $this->selfConfig = config('xfyun_voice') ?? [];
    }

    /**
     * 返回实例
     * */
    static public function getInstance()
    {
        self::$instance = new self();

        return self::$instance;
    }

    /**
     * 性别年龄识别 生成签名
     * */
    protected function sexAgeGenerateSign()
    {
        $parseUrl = parse_url($this->selfConfig['v2_igr']);
        $host = $parseUrl['host'];
        $date = gmdate('D, d M Y H:i:s') . ' UTC';
        $requestLine = 'GET ' . $parseUrl['path'] . ' HTTP/1.1';

        $signature = "host: " . $host . "\ndate: " . $date . "\n" . $requestLine;
        $signature = hash_hmac('sha256', $signature, $this->selfConfig['apiSecret'], true);
        $signature = base64_encode($signature);

        $authorization = 'api_key="' . $this->selfConfig['apiKey'] . '",algorithm="hmac-sha256",headers="host date request-line",signature="' . $signature . '"';
        $authorization = base64_encode($authorization);

        return 'authorization=' . $authorization . '&date=' . urlencode($date) . '&host=' . $host;
    }

    /**
     * 性别年龄识别 鉴权
     * */
    public function sexAgeAuthorization()
    {
        $generateSign = $this->sexAgeGenerateSign();
        $requestUrl = $this->selfConfig['v2_igr'] . '?' . $generateSign;

        return ['requestUrl' => $requestUrl];
    }

    public function __destruct()
    {
        // TODO: Implement __destruct() method.
    }
}